package com.daocao.yfb.fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.daocao.yfb.R;
import com.daocao.yfb.activity.MsgTipsActivity;
import com.daocao.yfb.activity.WebViewActivity;
import com.daocao.yfb.adapter.HomeAdapter;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.model.IndexModel;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.SpaceItemDecoration;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.util.Util;
import com.daocao.yfb.view.BannerLayout;

import static com.daocao.yfb.util.Util.getStatusBarHeight;

/**
 * Created by xsooy-pc on 17-12-4.
 */

public class MainHomeFragment extends BaseFragment {
    @BindView(R.id.banner)
    BannerLayout banner;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    Unbinder unbinder;
    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.img_msg_tips)
    ImageView imgMsgTips;
    private HomeAdapter adapter;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_main_home;
    }

    @Override
    protected void initView() {
        viewTop.post(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    int height = getStatusBarHeight(getContext());
                    if (height == 0) {
                        return;
                    }
                    ViewGroup.LayoutParams lp = viewTop.getLayoutParams();
                    lp.height = height;
                    viewTop.setLayoutParams(lp);
                }
            }
        });
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getIndex();
            }
        });
        adapter = new HomeAdapter(getContext());
        GridLayoutManager lm = new GridLayoutManager(getContext(), 3);
        list.setLayoutManager(lm);
        int x10 = getResources().getDimensionPixelSize(R.dimen.x10);
        list.addItemDecoration(new SpaceItemDecoration(x10));
        list.setAdapter(adapter);
        getIndex();
    }


    public void getIndex() {

        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
//        ((MainActivity) getActivity()).loadingDialog.show();
        refresh.setRefreshing(true);
        Api.api_service.getIndex().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
//                        ((MainActivity) getActivity()).loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });

    }

    private void setData(IndexModel.DatasBean datas) {
        adapter.update(datas.goods);
        if (datas.banners == null || datas.banners.size() == 0) {
            return;
        }

        List<String> bannerData = new ArrayList<>();
        for (int i = 0; i < datas.banners.size(); i++) {
            bannerData.add(datas.banners.get(i).image_url);
        }
        banner.setViewUrls(bannerData);
        banner.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("url", datas.banners.get(position).link);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.img_msg_tips)
    public void onViewClicked() {
        Util.startActivity(getActivity(), MsgTipsActivity.class);
    }
}
